﻿using Dapper;
using Mall.DomainModel;
using Mall.DomainModel.Dto.Query;
using Mall.Repository.Repository;
using Microsoft.Data.SqlClient;
using Newtonsoft.Json.Linq;
using System.Data;
using System.Linq.Expressions;
using System.Text;

namespace Mall.Repository
{
    public class ShipperManageUserRepository : BaseRepository<ShipperManageUser, long>,IShipperManageUserRepository
    {
        public ShipperManageUserRepository(IDapperConfiguration configuration) : base(configuration)
        {
        }

        public async Task<Tuple<IList<ShipperManageUser>,int>> GetList(IQueryObject<ShipperManageUser> query)
        {
            var sql = new StringBuilder();
            sql.Append("select * from ShipperManageUsers");
            var sqlCount = new StringBuilder();
            sqlCount.Append("SELECT COUNT(Id) FROM ShipperManageUsers");
            var querys = query.QuerySql.FirstOrDefault();
            if (query.QuerySql.Count > 0)
            {
                sql.Append(string.Format(querys.Key, " GROUP BY Id"));
                sqlCount.Append(string.Format(querys.Key, ""));
            }
            //分页
            query.Skip = query.Skip < 0 ? 0 : query.Skip;
            query.Count = query.Count <= 0 ? int.MaxValue : query.Count;
            sql.Append(" ORDER BY CreationTime DESC   OFFSET " + query.Skip + " ROWS FETCH NEXT " + query.Count + " ROWS ONLY");
            using (ReadConnection)
            {
                var list = await ReadConnection.QueryAsync<ShipperManageUser>(sql.ToString(), querys.Value);
                var count = await ReadConnection.QuerySingleAsync<int>(sqlCount.ToString(), querys.Value);
                return new Tuple<IList<ShipperManageUser>,int>(list.ToList(), count);
            }
        }
        public async Task<bool> Add(ShipperManageUser user)
        {
            var sql = @"INSERT INTO ShipperManageUsers (Id, RealName, Phone, UserName, Password, RoleNames, IsEnabled, CreatorUserId, CreationTime) VALUES (@Id, @RealName, @Phone, @UserName, @Password, @RoleNames, @IsEnabled, @CreatorUserId, @CreationTime)";

            // 3. 使用DynamicParameters确保参数传递
            var parameters = new DynamicParameters();
            parameters.Add("Id", user.Id);
            parameters.Add("RealName", user.RealName);
            parameters.Add("Phone", user.Phone);
            parameters.Add("UserName", user.UserName);
            parameters.Add("Password", user.Password);
            parameters.Add("RoleNames", user.RoleNames);
            parameters.Add("IsEnabled", user.IsEnabled);
            parameters.Add("CreatorUserId", user.CreatorUserId);
            parameters.Add("CreationTime", user.CreationTime);

            // 4. 执行插入
            using (WriteConnection)
            {
                var add= await WriteConnection.ExecuteAsync(sql, parameters);
                return add > 0;
            }
        }

        public async Task<bool> Update(ShipperManageUser user)
        {
            var sql = @"UPDATE ShipperManageUsers SET RealName = @RealName,Phone = @Phone,UserName = @UserName,Password = @Password,RoleNames = @RoleNames,IsEnabled = @IsEnabled,EditPassWordTime = @EditPassWordTime,LastModifierUserId = @LastModifierUserId,LastModificationTime = @LastModificationTime WHERE Id = @Id";  // 确保通过Id定位要更新的记录

            // 3. 使用DynamicParameters确保参数传递
            var parameters = new DynamicParameters();
            parameters.Add("Id", user.Id);  // 明确指定DbType
            parameters.Add("RealName", user.RealName);
            parameters.Add("Phone", user.Phone);
            parameters.Add("UserName", user.UserName);
            parameters.Add("Password", user.Password);
            parameters.Add("RoleNames", user.RoleNames);
            parameters.Add("IsEnabled", user.IsEnabled);
            parameters.Add("EditPassWordTime", user.EditPassWordTime);
            parameters.Add("LastModifierUserId", user.LastModifierUserId);
            parameters.Add("LastModificationTime", user.LastModificationTime);

            // 4. 执行插入
            using (WriteConnection)
            {
               var updadte=await WriteConnection.ExecuteAsync(sql, parameters);
               return  updadte > 0;
            }
        }
    }
}
